Page({
  data: {
    complaints: [],
    page: 1,
    pageSize: 10,
    hasMore: true,
    isLoading: false
  },

  onLoad: function() {
    this.loadComplaints();
  },

  loadComplaints: function() {
    if (this.data.isLoading || !this.data.hasMore) return;
    
    this.setData({ isLoading: true });
    
    const app = getApp();
    app.request({
      url: '/complaints/',
      method: 'GET',
      data: {
        page: this.data.page,
        page_size: this.data.pageSize
      },
      success: (res) => {
        if (res.statusCode === 200) {
          const newComplaints = res.data;
          const complaints = this.data.page === 1 ? newComplaints : [...this.data.complaints, ...newComplaints];
          
          this.setData({
            complaints: complaints,
            hasMore: newComplaints.length === this.data.pageSize,
            page: this.data.page + 1,
            isLoading: false
          });
        } else {
          wx.showToast({
            title: '获取投诉列表失败',
            icon: 'none'
          });
          this.setData({ isLoading: false });
        }
      },
      fail: () => {
        wx.showToast({
          title: '网络请求失败',
          icon: 'none'
        });
        this.setData({ isLoading: false });
      }
    });
  },

  onReachBottom: function() {
    // 页面上拉触底事件的处理函数
    if (this.data.hasMore) {
      this.loadComplaints();
    }
  },

  goToComplaintDetail: function(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/technician/complaintDetail?id=${id}`
    });
  },

  onPullDownRefresh: function() {
    // 下拉刷新
    this.setData({
      page: 1,
      hasMore: true
    });
    this.loadComplaints();
    wx.stopPullDownRefresh();
  }
});